Yacon.data.DataSet = function(page){
	
	this.dataset = new Yacon.container.Table();	
	this.body = new Yacon.container.Record();
	this.action = new Yacon.container.Record();
	this.record = null;	
	this.rsp = null;
	
	if(!Yacon.isEmpty(page)){
		this.page = page;
		this.baseCreate();
	}
			
};

Yacon.data.DataSet.Extend(Object,{
	
	/* 创建 */
	baseCreate: function(){
		
		if(Yacon.isEmpty(this.page)){
			
			return;
			
		}else{
				
			this.appCode = this.page.element.appCode;
			this.pagePath = this.page.element.pagePath;
			this.classPath = this.page.element.classPath;
			this.contextPath =  this.page.element.contextPath;	
			
			this.setAction("className",this.classPath);
			this.setAction("pagePath",this.pagePath);
			
			this.setAction("serviceCode","buss");
			this.setAction("traceID","111111");
			this.setAction("type","0");
			
			/* 页面地址信息 */
			if(!Yacon.isEmpty(this.page.element.parameter)){
				
				var params = this.page.element.parameter;
				
				for(var item in params){
					this.setBody(item,params[item]);
				}
				
			}
			
		}
		
	},
	
	/* 创建记录 */
	createRecord: function(){
		this.record = this.dataset.createRecord();
		return this.record;
	},
	
	/* 设置字段值 */
	setFieldValue: function(fieldName,fieldValue){
		
		if(this.record == null){
			this.getFirstRecord();
		}
		
		this.record.setFieldValue(fieldName,fieldValue);
	},
	
	/* 获取JSON信息 */
	getJSON: function(){
		
		var dataCompact = new Yacon.dispatch.msg.TJsonBaseConverter();
		
		var actionJS = dataCompact.compactRecord(this.action);
		var bodyJS = dataCompact.compactRecord(this.body);
		var datasetJS = dataCompact.compactTable(this.dataset);
		
		return "{action:"+actionJS+",body:"+bodyJS+",dataset:"+datasetJS+"}";
		
	},
	
	getFieldValue:function(fieldName){	
		
		var fieldValue = this.record.getFieldValue(fieldName);
		
		return fieldValue;	
	},
	
	getFirstRecord:function() {
		this.record = this.dataset.getFirstRecord();
		
		if(this.record == undefined){
			this.record = this.createRecord();
		}
		
		return this.record;
	},
	
	setAction:function(attributeName,attributeValue){
		
		if(attributeValue == undefined){
			attributeValue = "";
		}
		
		this.action.setFieldValue(attributeName,attributeValue);
	},
	
	getAction:function(attributeName){
		return this.action.getFieldValue(attributeName);
	},
	
	setBody:function(attributeName,attributeValue){
		
		if(attributeValue == null || attributeValue == undefined){
			attributeValue = "";
		}
		
		if(Yacon.isString(attributeValue)){
			this.body.setFieldValue(attributeName,attributeValue);
		}else{
			this.body.setFieldValue(attributeName,attributeValue.toString());
		}
		
		
	},
	
	getBody:function(attributeName){
		return this.body.getFieldValue(attributeName);
	},
	
	/* 前后台交互 */
	switchMessage: function(methodName) {
		
		if(methodName){
			this.setAction("methodName",methodName);
		}else{
			this.setAction("methodName",this.defaultMethod);
		}
			
		var dispatch = new Yacon.TDispatch({
			url:this.contextPath+"/managerService",
			req:this.getJSON(),
			appCode : this.appCode
		});
		
		dispatch.post(false);
		
		this.rsp = dispatch.rsp;
		
		/* 处理返回信息 */
		var typ = this.rsp.action.type;
		
		if(!Yacon.isEmpty(typ)){
			if(typ == "0" || typ == 0){
				alert(this.rsp.action.message);
			}
		}
		
		/* 存在返回表 */
		var rspData = this.rsp.dataset;
		if(!Yacon.isEmpty(rspData)){
			
			this.clearDataSet();
			
			for(var i=0;i<rspData.length;i++){	
				
				var rspRecord = rspData[i];	
				
				this.createRecord();
				
				for(var item in rspRecord){
					
					this.setDataSet(item,rspRecord[item]);
				}
				
			}
			
			this.dataset.beforeFirst();
			
		}
		
		
	},
	
	getDataValue: function(){	
		return this.rsp.action.message;		
	},
	
	/* 弹窗 */
	switchWindow: function(element){
		
		var url;
		
		if(element){
			url = element.url;
		}else{
			url = "about:blank";
		}
		
		url = Yacon.addUrlParam("parentElement", this.elementID, url);
		
		if(!Yacon.isEmpty(element)){
			url = Yacon.addUrlParam("lilyWindowWidth" , element.width , url);
			url = Yacon.addUrlParam("lilyWindowHeight" , element.height , url);
		}
	
		var dispatch = new Yacon.TDispatch({
			url : url,
			appCode : this.appCode
		});
		
		dispatch.postPage();
		
		this.rsp = dispatch.rsp;
		
	},
	
	switchPage: function(url){
		
		var dispatch = new Yacon.TDispatch({
			url : url,
			appCode : this.appCode
		});
		
		dispatch.postPage();
		
		this.rsp = dispatch.rsp;
	},
	
	free:function(){
		
		if(this.action){
			this.action.free();
		}
		
		if(this.dataset){
			this.dataset.free();
		}
		
		if(this.body){
			this.body.free();
		}
		
		if(this.record){
			this.record.free();
		}
		
	},	
	
	/* 设置数据集 */
	setDataSet: function(fieldName,fieldValue,fieldOldValue){
		
		if(Yacon.isEmpty(fieldName)){
			return;
		}
		
		if(fieldValue == undefined){
			return;
		}
		
		if(Yacon.isEmpty(fieldValue)){
			fieldValue = "";
		}
		
		if(Yacon.isEmpty(fieldOldValue)){
			fieldOldValue = fieldValue;
		}
		
		var record = new Yacon.container.Record();
		
		record.setFieldValue("fieldvalue",fieldValue);
		record.setFieldValue("fieldoldvalue",fieldOldValue);
		
		this.setFieldValue(fieldName,record);
		
	},
	
	/* 清空DataSet */
	clearDataSet: function(){
		delete this.dataset;
		this.dataset = new Yacon.container.Table();	
	},
	
	/* 加载数据 */
	instData:function(){
		this.component.instData();
	},
	
	/* 展示数据 */
	showData:function(){
		this.component.showData();
	},
	
	getDataSet: function(choice){
		
		if(Yacon.isEmpty(choice)){
			choice = "flat";
		}
		
		this.dataset = new Yacon.container.Table();	
		
		var search = "div[lily-id='"+this.component.elementID+"'][lily-type='"+this.component.elementType+"']";
		
		if(choice == "flat"){
			
			var datas = $(search+" tbody tr.active");
			
			for(var i=0;i<datas.length;i++){
				
				this.createRecord();			
				var tds = datas.eq(i).find("td");			
				for(var j=0;j<tds.length;j++){
					
					var fieldName = tds.eq(j).attr("field_name");
					var fieldValue = tds.eq(j).attr("field_val");
					var fieldOldValue = tds.eq(j).attr("field_old_val");
					
					if(Yacon.isEmpty(fieldOldValue)){
						this.setDataSet(fieldName,fieldValue);
					}else{
						this.setDataSet(fieldName,fieldValue,fieldOldValue);
					}
						
				}
						
			}
			
		}else{
			
			var checks = $(search+" .panel-body .table-body input:checked").parent().parent().parent();
			
			for(var i=0;i<checks.length;i++){
			
				this.createRecord();			
				var tds = checks.eq(i).find("td");			
				for(var j=0;j<tds.length;j++){
					
					var fieldName = tds.eq(j).attr("field_name");
					var fieldValue = tds.eq(j).attr("field_val");
					
					var fieldOldValue = tds.eq(j).attr("field_old_val");
					
					if(Yacon.isEmpty(fieldOldValue)){
						this.setDataSet(fieldName,fieldValue);
					}else{
						this.setDataSet(fieldName,fieldValue,fieldOldValue);
					}
						
				}
						
			}
			
		}
		
		/* 从表 */
		if(!Yacon.isEmpty(this.component.view.grid.master)){
			
			var masterID = this.component.view.grid.master.id;
			var parentGrid = this.page.find(masterID);
			parentGrid.getDataSet();
			
			var params = this.component.view.grid.master.fields;		
			for(var i=0;i<params.length;i++){
				var parentKey = params[i].fieldName;
				var activekey = params[i].foreignName;				
				this.setBody(activekey,parentGrid.getFieldValue(parentKey));			
			}
			
		}
			
		return this.dataset;
		
	},
	
	getCheckDataSet: function(){
		
		this.dataset = new Yacon.container.Table();	
		
		var choice = this.component.view.grid.choice;
		
		var search = "div[lily-id='"+this.component.elementID+"'][lily-type='"+this.component.elementType+"']";
		
		if(choice == "flat"){
			
			var datas = $(search+" tbody tr.active");
			
			for(var i=0;i<datas.length;i++){
				
				this.createRecord();			
				var tds = datas.eq(i).find("td");			
				for(var j=0;j<tds.length;j++){
					
					var fieldName = tds.eq(j).attr("field_name");
					var fieldValue = tds.eq(j).attr("field_val");
					
					var fieldOldValue = tds.eq(j).attr("field_old_val");
					
					if(Yacon.isEmpty(fieldOldValue)){
						this.setDataSet(fieldName,fieldValue);
					}else{
						this.setDataSet(fieldName,fieldValue,fieldOldValue);
					}
					
						
				}
						
			}
			
		}else{
			
			var checks = $(search+" .panel-body .table-body input:checked").parent().parent().parent();
			
			for(var i=0;i<checks.length;i++){
			
				this.createRecord();			
				var tds = checks.eq(i).find("td");			
				for(var j=0;j<tds.length;j++){
					
					var fieldName = tds.eq(j).attr("field_name");
					var fieldValue = tds.eq(j).attr("field_val");
					
					var fieldOldValue = tds.eq(j).attr("field_old_val");
					
					if(Yacon.isEmpty(fieldOldValue)){
						this.setDataSet(fieldName,fieldValue);
					}else{
						this.setDataSet(fieldName,fieldValue,fieldOldValue);
					}
						
				}
						
			}
			
		}
		
		this.dataset.beforeFirst();
		
		/* 从表 */
		if(!Yacon.isEmpty(this.component.view.grid.master)){
			
			var masterID = this.component.view.grid.master.id;
			var parentGrid = this.page.find(masterID);
			parentGrid.getDataSet();
			
			var params = this.component.view.grid.master.fields;		
			for(var i=0;i<params.length;i++){
				var parentKey = params[i].fieldName;
				var activekey = params[i].foreignName;				
				this.setBody(activekey,parentGrid.getFieldValue(parentKey));			
			}
			
		}
			
		return this.dataset;
		
	},
	
	getDataSetValue: function(fieldName){
				
	    var active_field = this.dataset.getFieldValue(fieldName);
	
	    if(active_field){
		    return active_field.getFieldValue("fieldvalue");
		}

		return "";
		
	},
	
	getDataSetOldValue: function(fieldName){
		
		var active_field = this.dataset.getFieldValue(fieldName);
		
	    if(active_field){
		    return active_field.getFieldValue("fieldoldvalue");
		}

		return "";
		
	},
	
	/* 刷新数据 */
	refresh:function(){
		
		if(this.refreshMethod){
			this.setAction("methodName",this.refreshMethod);
		}
		
		else{
			this.setAction("methodName","refresh");
		}
		
		var dispatch = new Yacon.TDispatch({
			url : "/"+this.appCode+"/managerService",
			req : this.getJSON(),
			before:{
				method:"_lily_beforeRefresh",
				param:[this]
			},
			after:{
				method:"_lily_afterRefresh",
				param:[this]
			},
			dataSet : this,
			appCode : this.appCode
		});
		
		dispatch.post(true);
				
	},
	
	setWhereField: function(fieldName,fieldValue){
		this.setDataSet(fieldName,fieldValue,fieldValue);
	},
	
	getDataSetCount: function(){	
		return this.dataset.getRecordCount();		
	},
	
	next: function(){
		
		if(this.dataset){
			return this.dataset.next();
		}
		
		return false;
		
	},
	
	getUrl:function(){
		
		var url = this.contextPath + "/managerService?switch_xml="+this.getJSON();
		
		url = Yacon.addToken(url);
		
		return url;
		
	},
	
	isEdit: function(){
		var type =  this.getAction("type");
		if(type == "2" || type == 2){
			return true;
		}else{
			return false;
		}
	},
	
	isInsert: function(){
		var type =  this.getAction("type");
		if(type == "1" || type == 1){
			return true;
		}else{
			return false;
		}
	},
	
	isDelete: function(){
		var type =  this.getAction("type");
		if(type == "3" || type == 3){
			return true;
		}else{
			return false;
		}
	},
	
	isSuccess: function(){
		var result = this.rsp.action.result;
		if(result == "0000"){
			return true;
		}else{
			return false;
		}
	}

});

function _lily_beforeRefresh(dataSet){
	
	/* 过屏 */
	if(!Yacon.isEmpty(dataSet.component.spinner)){
		dataSet.component.spinShow();
	} 
	
	Yacon.callBack(dataSet.component.elementID+"_beforeRefresh", [dataSet.component,dataSet.page]);
	
};

function _lily_afterRefresh(dataSet){
	
	if(dataSet.component.elementType == "tree"){
		
	}else{
		
		var typ = dataSet.rsp.action.type;
		
		if(!Yacon.isEmpty(typ)){
			if(typ == "0" || typ == 0){
				
			}else{
				dataSet.component.refreshElement();
			}
		}
			
	}	

	Yacon.callBack(dataSet.component.elementID+"_afterRefresh", [dataSet.component,dataSet.page]);
	
	/* 过屏 */
	if(!Yacon.isEmpty(dataSet.component.spinner)){
		dataSet.component.spinHidden();
	}
};
